package com.sdp.right.db.sys_system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.core.bizc.utils.ApplicationContextUtils;
import com.sdp.right.db.sys_config.service.impl.SysConfigServiceImpl;
import com.sdp.right.db.sys_cron.service.impl.SysCronServiceImpl;
import com.sdp.right.db.sys_cron_log.service.impl.SysCronLogServiceImpl;
import com.sdp.right.db.sys_dict.service.impl.SysDictServiceImpl;
import com.sdp.right.db.sys_entity_info.service.impl.SysEntityInfoServiceImpl;
import com.sdp.right.db.sys_flow_current_task.service.impl.SysFlowCurrentTaskServiceImpl;
import com.sdp.right.db.sys_flow_instance.service.impl.SysFlowInstanceServiceImpl;
import com.sdp.right.db.sys_flow_node_attribute_version.service.impl.SysFlowNodeAttributeVersionServiceImpl;
import com.sdp.right.db.sys_flow_node_jump_version.service.impl.SysFlowNodeJumpVersionServiceImpl;
import com.sdp.right.db.sys_flow_task_his.service.impl.SysFlowTaskHisServiceImpl;
import com.sdp.right.db.sys_flow_template.service.impl.SysFlowTemplateServiceImpl;
import com.sdp.right.db.sys_flow_template_version.service.impl.SysFlowTemplateVersionServiceImpl;
import com.sdp.right.db.sys_import_template.service.impl.SysImportTemplateServiceImpl;
import com.sdp.right.db.sys_menu.service.impl.SysMenuServiceImpl;
import com.sdp.right.db.sys_menu_button.service.impl.SysMenuButtonServiceImpl;
import com.sdp.right.db.sys_oper_log.service.impl.SysOperLogServiceImpl;
import com.sdp.right.db.sys_page.service.impl.SysPageServiceImpl;
import com.sdp.right.db.sys_rdp_report.service.impl.SysRdpReportServiceImpl;
import com.sdp.right.db.sys_role_menu.service.impl.SysRoleMenuServiceImpl;
import com.sdp.right.db.sys_system.mapper.SysSystemMapper;
import com.sdp.right.db.sys_system.service.ISysSystemService;
import com.sdp.right.entity.SysSystem;
import com.sdp.right.service.RemoveSystemService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2023-10-16
 */
@Service
@DS(value="right")
public class SysSystemServiceImpl extends ServiceImpl<SysSystemMapper, SysSystem> implements ISysSystemService {
	
	//观察者
	List<RemoveSystemService> removeSystemobserver = new ArrayList<RemoveSystemService>();
	
	@PostConstruct
	public void init() {
		
		//删除此系统相关的菜单信息
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysMenuServiceImpl.class));
		
		//删除菜单按钮信息
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysMenuButtonServiceImpl.class));
		
		//删除操作日志信息
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysOperLogServiceImpl.class));
		
		//删除自定义页面信息
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysPageServiceImpl.class));
		
		//删除RDP报表信息
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysRdpReportServiceImpl.class));
		
		//删除此系统中的角色权限信息
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysRoleMenuServiceImpl.class));
		
		//删除此系统中的数据字典信息
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysDictServiceImpl.class));
		
		//删除此系统中的定时任务信息
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysCronServiceImpl.class));
		
		//删除此系统中的定时任务日志信息
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysCronLogServiceImpl.class));
		
		//删除此系统中的实体信息
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysEntityInfoServiceImpl.class));
		
		//删除此系统中的导入导出信息
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysImportTemplateServiceImpl.class));
		
		//删除工作流信息
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysFlowTemplateServiceImpl.class));
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysFlowTemplateVersionServiceImpl.class));
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysFlowCurrentTaskServiceImpl.class));
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysFlowTaskHisServiceImpl.class));
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysFlowInstanceServiceImpl.class));
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysFlowNodeAttributeVersionServiceImpl.class));
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysFlowNodeJumpVersionServiceImpl.class));
	
		//系统参数
		removeSystemobserver.add(ApplicationContextUtils.getBean(SysConfigServiceImpl.class));
	}

	@Override
	public void removeSystem(String systemNo) {
		LambdaQueryWrapper<SysSystem> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysSystem::getSystemNo, systemNo);
		remove(queryWrapper);
		
		for(RemoveSystemService rss : removeSystemobserver) {
			rss.removeBySystemNo(systemNo);
		}
	}

	@Override
	public List<SysSystem> querySysSystemByNo(String systemNo) {
		LambdaQueryWrapper<SysSystem> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysSystem::getSystemNo, systemNo);
		return list(queryWrapper);
	}

	@Override
	public SysSystem querySysSystem(String systemNo) {
		LambdaQueryWrapper<SysSystem> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysSystem::getSystemNo, systemNo);
		return getOne(queryWrapper,false);
	}

}
